package com.kuang.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
@RestController
public class JDBCController {
    @Autowired
    JdbcTemplate jdbcTemplate;
    //直接用模板写
    //查询所有数据列表(没有实体类，用万能的map来获取数据)
    String sql="select *from mybatis.user;";
    @GetMapping("/userList")
    public List<Map<String,Object>>listMap(){
        List<Map<String, Object>> mapList = jdbcTemplate.queryForList(sql);
        return mapList;
    }
    //添加
    @GetMapping("/addUser")
    public String addUser(){
String sql="insert into mybatis.user(id,name,pwd) values(5,'米迷','123456')";
jdbcTemplate.update(sql);
return "add ok!";
    }
    //修改
    @GetMapping("/update/{id}")
    public String updateUser(@PathVariable("id") int id){
       //数据库操作语句
        String sql="update mybatis.user set name=?,pwd=? where id="+id;
        //修改的数据
        Object[] objects=new Object[2];
        objects[0]="宇明";
        objects[1]="122223";
        jdbcTemplate.update(sql,objects);
        return "update ok!";
    }
    //删除
    @GetMapping("/delete/{id}")
    public String delete(@PathVariable("id")int id){
String sql="delete from mybatis.user where id=?";
jdbcTemplate.update(sql,id);
return "delete ok!";
    }


}
